package abcSnake;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.*;

public class Yard extends JPanel{
	   /**
	 * 
	 */
	private static final long serialVersionUID = -4044801959114041679L;
	Snake snake;
	   Egg   egg;
	   int blockSize=Game.getBlockSize();
	   long Score=0;
	   boolean pause=false;
	   boolean havaPaint=true;
	   Image bg;
	   Image head;
	
	   
	   
	Yard(){
		snake=new Snake();
		egg=new Egg();
		bg=new ImageIcon("image//bacgroud.jpg").getImage();
		head=new ImageIcon("image//head.png").getImage();
		
		
       // ImageIcon img1=new ImageIcon(getClass().getResource("/image/bacgroud.jpg")); 
       // ImageIcon img2=new ImageIcon(getClass().getResource("/image/head.png")); 
        
		//bg=img1.getImage();
		//head=img2.getImage();
		
		
	
	}
	
	

	
	  public void paint(Graphics g){
		 Graphics2D g2d=(Graphics2D) g;
		 g2d.drawImage(bg, 0, 0,this);
		 g2d.setColor(Color.lightGray);
		 for(int i=0;i<=30;i++)
		 g2d.drawLine(0, Game.getBlockSize()*i, 800, Game.getBlockSize()*i);
		 for(int i=0;i<=40;i++)
			 g2d.drawLine((Game.getBlockSize())*i, 0, Game.getBlockSize()*i, 600);
		
		 
			 if(egg.alive==false)
			 egg.appear();
			
			 //g2d.fillRect(egg.getX()*blockSize, egg.getY()*blockSize, blockSize, blockSize);
			 g2d.drawImage(head,egg.getX()*blockSize, egg.getY()*blockSize, blockSize, blockSize,this);	
			 egg.changeColor();
			 
			 egg.alive=true;
		 
		 if(snake.getAlive()==true){
			 for(int i=0;i<snake.body.size();i++){
				 Dimension body=snake.body.get(i);
				 g2d.drawImage(head, (int)body.getWidth()*blockSize, (int)body.getHeight()*blockSize, blockSize, blockSize,this);				 
				 //g2d.fillRect((int)body.getWidth()*blockSize, (int)body.getHeight()*blockSize, blockSize, blockSize);
				  }
			 if(pause==false)
			 snake.move();
			 if(snake.head.width==egg.getX()&&snake.head.height==egg.getY()){
				 Score+=(snake.len+1)*(snake.len+1)/2*100*19;
				 snake.grow();
			 egg.alive=false;
			 System.out.println(snake.body.size());
			 }
			 
			 g2d.setColor(Color.RED);
			 g2d.setFont(new Font("楷体", Font.PLAIN, 30));
			 g2d.drawString("SCORE="+Score,10,30);
			 
		 }
		 if(snake.getAlive()==false){
			 g2d.setColor(Color.GRAY);
			 g2d.setFont(new Font("楷体", Font.PLAIN, 90));
			 g2d.drawString("GAME OVER !",150,250);
			 g2d.drawString("SCORE="+Score,150,350);
		 }
		
		 havaPaint=true;
	  }


}
